# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from common.models.agents import AgentSquareModel
from common.models.agents import AgentChatRoomingModel
from common.models.agents import AgentChatSessionModel
from common.models.agents import AgentChatRecordsModel
from common.models.agents import AgentRobotTeamedModel
from common.models.agents import AgentRobotCollectModel
from common.models.agents import AgentRobotPublishModel
from common.models.agents import AgentRobotInstructModel
from common.models.agents import AgentKnowTeamedModel
from common.models.agents import AgentKnowArchiveModel
from common.models.agents import AgentKnowSplittingModel
from common.postgres.public import PgKnowledgeModel


class CleanLogic:
    @classmethod
    async def clean_robot(cls, robot_id: int):
        """ 清理智能体关联表 """
        # 当前时间
        curr_time = int(time.time())

        # 协作团队
        await AgentRobotTeamedModel.filter(id=robot_id).delete()

        # 快捷指令
        await AgentRobotInstructModel.filter(id=robot_id).delete()

        # 发布记录
        await AgentRobotPublishModel.filter(robot_id=robot_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

        # 应用窗口
        await AgentChatRoomingModel.filter(robot_id=robot_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

        # 会话窗口
        await AgentChatSessionModel.filter(robot_id=robot_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

        # 对话记录
        await AgentChatRecordsModel.filter(robot_id=robot_id, is_delete=0).update(user_delete=1, delete_time=curr_time)

        # 广场分享
        await AgentSquareModel.filter(robot_id=robot_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

        # 广场收藏
        await AgentRobotCollectModel.filter(robot_id=robot_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

    @classmethod
    async def clean_know(cls, know_id: int):
        """ 清理知识库管理表 """
        # 当前时间
        curr_time = int(time.time())

        # 协作团队
        await AgentKnowTeamedModel.filter(id=int(know_id)).delete()

        # 知识拆分
        await AgentKnowSplittingModel.filter(know_id=know_id).delete()

        # 知识文档
        await AgentKnowArchiveModel.filter(know_id=know_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

        # 知识数据
        await PgKnowledgeModel.filter(know_id=know_id, is_delete=0).update(is_delete=1, delete_time=curr_time)

    @classmethod
    async def clean_know_archive(cls, archive_id: int):
        """ 清理文档关联表 """
        # 当前时间
        curr_time = int(time.time())

        # 知识拆分
        await AgentKnowSplittingModel.filter(archive_id=archive_id).delete()

        # 知识数据
        await PgKnowledgeModel.filter(archive_id=archive_id, is_delete=0).update(is_delete=1, delete_time=curr_time)
